package com.ccys.cms.service.thinkWell.impl;


import cn.hutool.core.util.IdUtil;
import com.ccys.cms.service.thinkWell.TblPhqDetailWebService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.thinkWell.TblPhqDetailDTO;
import com.ccys.db.entity.TblPhqDetail;
import com.ccys.db.entity.TblPhqDetailResponse;
import com.ccys.db.mapper.TblPhqDetailMapper;
import com.ccys.db.mapper.TblPhqDetailResponseMapper;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.thinkWell.TblPhqDetailWebVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * tbl_phq_detail管理
 */
@Service
@Slf4j
public class TblPhqDetailWebServiceImpl extends BaseServiceImpl<TblPhqDetail> implements TblPhqDetailWebService {

    @Autowired(required = false)
    private TblPhqDetailMapper tblPhqDetailMapper;
    @Autowired(required = false)
    private TblPhqDetailResponseMapper tblPhqDetailResponseMapper;
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;
    @Override
    public ResponseResult listObjectsCms(HttpServletRequest request, HttpServletResponse response, TblPhqDetailDTO dto) {
        if(StringUtil.isEmpty(dto.getPhqId())) {
            return ResponseResult.error("phqId is not null");
        }
        PageUtils.startPage(request);
        List<TblPhqDetailWebVO> list = this.tblPhqDetailMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


    @Override
    public ResponseResult change(TblPhqDetailDTO entity) {
        if (StringUtils.isBlank(entity.getId())) {
            if(entity.getStatus() == null) {
                entity.setStatus(1);
            }
            // 新增
            this.saveEntity(entity);
            List<TblPhqDetailResponse> responseList = entity.getResponseList();
            try {
                for (TblPhqDetailResponse r : responseList) {
                    r.setDetailId(entity.getId());
                    r.setCreatedOnUtc(System.currentTimeMillis());
                    r.setCreatedBy(UserUtil.getCurLoginUserId());
                    r.setId(IdUtil.randomUUID());
                    r.setDelFlag(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.tblPhqDetailResponseMapper.batchInsert(responseList);
        } else {
            // 编辑
            this.updateEntityById(entity);
            if(entity.getStatus() != null) {
                this.baseTblAuditTrailWebService.saveStatusLog(entity,entity.getStatus());
            }
            TblPhqDetailResponse tblPhqDetailResponse = new TblPhqDetailResponse();
            tblPhqDetailResponse.setDetailId(entity.getId());
            this.tblPhqDetailResponseMapper.delete(tblPhqDetailResponse);
            List<TblPhqDetailResponse> responseList = entity.getResponseList();
            try {
                for (TblPhqDetailResponse r : responseList) {
                    r.setDetailId(entity.getId());
                    r.setCreatedOnUtc(System.currentTimeMillis());
                    r.setCreatedBy(UserUtil.getCurLoginUserId());
                    r.setId(IdUtil.randomUUID());
                    r.setDelFlag(0);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            this.tblPhqDetailResponseMapper.batchInsert(responseList);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult delete(TblPhqDetail dto) {
        TblPhqDetail entity = new TblPhqDetail();
        entity.setId(dto.getId());
        entity.setDelFlag(1);
        int i = this.updateEntityById(entity);
        if(i > 0) {
            TblPhqDetailResponse tblPhqDetailResponse = new TblPhqDetailResponse();
            tblPhqDetailResponse.setDetailId(entity.getId());
            this.tblPhqDetailResponseMapper.delete(tblPhqDetailResponse);
        }
        return ResponseResult.success();
    }


}